package com.zidian.study.activity;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import com.zidian.student.trucks.R;
import com.zidian.study.adapter.Top10ListAdapter;
import com.zidian.study.bean.TopTagsBean;
import com.zidian.study.globa.Constant;
import com.zidian.study.globa.ToolBarActivity;
import com.zidian.study.utils.GetUserInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by GongCheng on 2016/11/30.
 */

public class TagTopActivity extends ToolBarActivity {
    @Bind(R.id.top10_list_rv)
    RecyclerView topListRv;

    private Top10ListAdapter adapter;
    private List<TopTagsBean.DataBean> data;
    private String teacherId;
    private String headPortrait;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarView(R.layout.toolbar_left);
        setContentView(R.layout.activity_tag_top);
        ButterKnife.bind(this);
        setTitle("教师热词");
        topListRv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new Top10ListAdapter();
        topListRv.setAdapter(adapter);
        data = new ArrayList<>();
        teacherId = getIntent().getStringExtra("teacherId");
        headPortrait = getIntent().getStringExtra("headPortrait");
        adapter.setHeadPortrait(headPortrait);
        getTopTags();
    }

//    private void testTags() {
//        List<String> tags = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            tags.add("老师的标签" + i);
//        }
//        adapter.setTags(tags);
//    }

    private void getTopTags() {
        String url = Constant.URL_TEACHER + "EvaluateByStudent/selectLabelTopTen";
        Map<String, String> map = new HashMap<>();
        map.put("teacherId", teacherId);
        OkHttpUtils.post().url(url).params(map).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                Snackbar.make(topListRv, "获取标签失败",Snackbar.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String response, int id) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.getInt("code") == 200) {
                        Gson gson = new Gson();
                        TopTagsBean bean = gson.fromJson(response, TopTagsBean.class);
                        data.addAll(bean.getData());
                        adapter.setTags(sortTags(data));
                    } else {
                        Snackbar.make(topListRv, jsonObject.getString("message"),Snackbar.LENGTH_SHORT).show();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     *
     * @param list
     * @return 排序后的标签名
     */
    public List<String> sortTags(List<TopTagsBean.DataBean> list){
        List<String> tags = new ArrayList<>();
        Collections.sort(list, new Comparator<TopTagsBean.DataBean>() {
            @Override
            public int compare(TopTagsBean.DataBean arg0, TopTagsBean.DataBean arg1) {
                return arg0.getLabelTop() - arg1.getLabelTop() ;
            }
        });
        for (int i = 0; i < list.size(); i++) {
            tags.add(list.get(i).getLabelName());
        }
        return tags;
    }
}
